
package com.sunny.hp.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;


public class RequestUtils {
	public static String[] getSTRFilterArr(String str[]){
		String str_imsi   = "";
		//        String[] filter_word = {"\'","\"","\\@","\\#","\\$","\\%","\\&","\\*","\\(","\\)","\\+","\\=","\\|","\\\\","\\}","\\]","\\{","\\[","\\\"","\\'","\\:","\\;","\\<","\\,","\\>","\\.","\\?","\\/"};
		String[] filter_word = {"\'","\"","\\/\\*","\\'","\\;","\\*\\/"};
		//        String[] filter_word = {"\'","\"","\\/\\*","\\'","\\*\\/","\\#"};
		for (int j=0;j < str.length;j++) {
			for(int i=0;i<filter_word.length;i++){
				str_imsi = str[j].replaceAll(filter_word[i],"");
				str[j] = str_imsi;
			}
			//            str[j].replaceAll("\\<", "&lt;");
			//            str[j].replaceAll("\\>", "&gt;");
		}

		return str;
	}
	public static String getSTRFilter(String str){
		String str_imsi   = "";
		//      String[] filter_word = {"\'","\"","\\@","\\#","\\$","\\%","\\&","\\*","\\(","\\)","\\+","\\=","\\|","\\\\","\\}","\\]","\\{","\\[","\\\"","\\'","\\:","\\;","\\<","\\,","\\>","\\.","\\?","\\/"};
		String[] filter_word = {"\'","\"","\\/\\*","\\'","\\;","\\*\\/"};
		//         String[] filter_word = {"\'","\"","\\/\\*","\\'","\\*\\/","\\#"};
		for(int i=0;i<filter_word.length;i++){
			str_imsi = str.replaceAll(filter_word[i],"");
			str = str_imsi;
		}
		//         str.replaceAll("\\<", "&lt;");
		//         str.replaceAll("\\>", "&gt;");
		return str;
	}
	// Request의 Parameter 전체를 Map 형태로 반환한다.
	public static Map getMap(HttpServletRequest request) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();

		if(NVL(request.getParameter("bm"),"N").equals("Y")){
			//FlashMap에 저장되어 전달된 값을 가져온다. 
			Map<String, ?> flashMap = RequestContextUtils.getInputFlashMap(request); 
			if(flashMap !=null) { 
				Set<String> keys = flashMap.keySet();
				for(String key : keys){
					param.put(key, flashMap.get(key));
				}
			}
		}else{
			Enumeration enums = request.getParameterNames();
			while (enums.hasMoreElements()) {
				String name = (String) enums.nextElement();
				String[] values = request.getParameterValues(name);
				if (values != null) {
					// 1개만 왔을 경우
					if (values.length >= 1){
						param.put(name, getSTRFilter(values[0]));
						param.put(name + "_arr", getSTRFilterArr(values));
					}else if (values.length < 1){
						param.put(name, "");
						param.put(name + "_arr", null);
					}
				} else
					param.put(name, "");
			}
		}
		return param;
	}

	// Request의 Parameter 전체를 Map 형태로 반환한다.
	public static Map getMap(MultipartHttpServletRequest request) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();

		Enumeration enums = request.getParameterNames();
		while (enums.hasMoreElements()) {
			String name = (String) enums.nextElement();
			String[] values = request.getParameterValues(name);

			if (values != null) {
				if (values.length >= 1){
					param.put(name, getSTRFilter(values[0]));
					param.put(name + "_arr", getSTRFilterArr(values));
				}else if (values.length < 1){
					param.put(name, "");
					param.put(name + "_arr", null);
				}
			} else
				param.put(name, "");
		}
		return param;
	}
	public static Map getMapNoEscape(HttpServletRequest request) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();


		Enumeration enums = request.getParameterNames();
		while (enums.hasMoreElements()) {
			String name = (String) enums.nextElement();
			String[] values = request.getParameterValues(name);

			if (values != null) {
				if (values.length >= 1){
					param.put(name, values[0]);
					param.put(name, values);
				}else if (values.length < 1){
					param.put(name, "");
					param.put(name + "_arr", null);
				}
			} else
				param.put(name, "");
		}
		return param;
	}
	// Request의 Parameter 전체를 Bean 형태로 반환한다.
	public static Object getBean(MultipartHttpServletRequest request, Class beanClass) throws Exception {
		Map properties = getMap(request);
		Object bean = beanClass.newInstance();
		BeanUtils.populate(bean, properties);
		//		HtmlSpecialCharacter.convert(bean);

		return bean;
	}
	// Request의 Parameter 전체를 Bean 형태로 반환한다.
	public static Object getBean(HttpServletRequest request, Class beanClass) throws Exception {
		Map properties = getMap(request);
		Object bean = beanClass.newInstance();
		BeanUtils.populate(bean, properties);
		//		HtmlSpecialCharacter.convert(bean);

		return bean;
	}

	public static Object getBeanNoEscape(HttpServletRequest request, Class beanClass) throws Exception {
		Map properties = getMapNoEscape(request);
		Object bean = beanClass.newInstance();
		BeanUtils.populate(bean, properties);
		//		HtmlSpecialCharacter.convert(bean);

		return bean;
	}

	// Request의 Parameter 전체를 Bean 형태로 반환한다.
	//	public static Object getBean(HttpServletRequest request, Class beanClass, String[] ignore) throws Exception
	//	{
	//		Map properties = getMap(request);
	//		Object bean = beanClass.newInstance();
	//		BeanUtils.populate(bean, properties);
	//		HtmlSpecialCharacter.convert(bean, ignore);
	//		return bean;
	//	}

	// Request의 특정 Parameter를 int 형태로 반환한다.
	public static int getIntParameter(HttpServletRequest request, String name) throws Exception {
		return getIntParameter(request, name, 0);
	}

	public static int getIntParameter(HttpServletRequest request, String name, int defaultValue) throws Exception {
		//int value = defaultValue;
		String valueStr = getStringParameter(request, name);
		try {
			defaultValue = Integer.parseInt(valueStr);
		} catch (NumberFormatException nfe) {
			return defaultValue;
		}
		return defaultValue;
	}

	// Request의 특정 Parameter를 long 형태로 반환한다.
	public static long getLongParameter(HttpServletRequest request, String name) throws Exception {
		return getLongParameter(request, name, 0);
	}

	public static long getLongParameter(HttpServletRequest request, String name, long defaultValue) throws Exception {
		//long value = defaultValue;
		String valueStr = getStringParameter(request, name);
		try {
			defaultValue = Long.parseLong(valueStr);
		} catch (NumberFormatException nfe) {
			return defaultValue;
		}
		return defaultValue;
	}

	// Request의 특정 Parameter를 double 형태로 반환한다.
	public static double getDoubleParameter(HttpServletRequest request, String name) throws Exception {
		return getDoubleParameter(request, name, 0);
	}

	public static double getDoubleParameter(HttpServletRequest request, String name, double defaultValue) throws Exception {
		//double value = defaultValue;
		String valueStr = getStringParameter(request, name);
		try {
			defaultValue = Double.parseDouble(valueStr);
		} catch (NumberFormatException nfe) {
			return defaultValue;
		}
		return defaultValue;
	}

	// Request의 특정 Parameter를 float 형태로 반환한다.
	public static float getFloatParameter(HttpServletRequest request, String name) throws Exception {
		return getFloatParameter(request, name, 0);
	}

	public static float getFloatParameter(HttpServletRequest request, String name, float defaultValue) throws Exception {
		//float value = defaultValue;
		String valueStr = getStringParameter(request, name);
		try {
			defaultValue = Float.parseFloat(valueStr);
		} catch (NumberFormatException nfe) {
			return defaultValue;
		}
		return defaultValue;
	}

	// Request의 특정 Parameter를 String 형태로 반환한다.
	public static String getStringParameter(HttpServletRequest request, String name) throws Exception {
		return getStringParameter(request, name, "");
	}

	public static String getStringParameter(HttpServletRequest request, String name, String defaultValue) throws Exception {
		String value = request.getParameter(name);
		if (value == null)
			return defaultValue;
		else
			return value;
	}

	/**
	 * NVL
	 * @param str, strDefault
	 * @return String
	 * @throws
	 */
	public static String NVL(String str, String strDefault)
	{
		String r = strDefault;
		try
		{
			if (str != null)
				r = str;
		}
		catch(Exception e)
		{
			//throw e;
		}
		return r;
	}
}
